#!/bin/bash
# .........................................................................
# Title: Master_Insurance.sh
#
# Main executable file for insurance section; submits all jobs via SLURM
# .........................................................................

# email notification setting
mailtype="ALL"

# job settings
nodes=1

# second argument is job name
job=${2}

# if user has shell profile setup
if [ -f ~/shell_profile.sh ]; then

    # run profile
    source ~/shell_profile.sh

    # set user variables
    U=${whoami}
    echo "User: "${U}
    
    # basic variables
    system_part="<SLURM_PARTITION>"

    # paths
    code_path="<CODE_PATH>"
    erroroutput_path="$project_path/erroroutput"

    # make error output folder if it doesn't exist
    mkdir -p "${erroroutput_path}"

    # job parameters
    partition_Insurance="<SLURM_PARTITION>"
    time_Insurance="0-48:00:00"
    ntasks_Insurance=4
    mem_Insurance="64000"

else

    echo "Please set up ~/shell_profile.sh file. Exiting."
    exitcd

fi

# sanity-checking that project_path variables exist
if [[ -z "$project_path" ]]; then
    echo "Empty project_path variable: Exiting..."
    exit 1
fi

#----------------
# Submit jobs
#----------------

# Enumerate insurers step
Enumerate_Insurers_ID=`sbatch \
         --partition=${partition_Insurance} --time=${time_Insurance} \
         --nodes=${nodes} --ntasks=${ntasks_Insurance} --job-name=Enumerate_Insurers \
		 --output="${erroroutput_path}/Enumerate_Insurers-%A_%a.out" --error="${erroroutput_path}/Enumerate_Insurers-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Insurance} \
         "${code_path}/insurance/Insurance_Stata_Controller.sh" ${U} Enumerate_Insurers | awk '{print $NF}'`
echo "Submitted Enumerate_Insurers Job: "${Enumerate_Insurers_ID}
sleep 1

# Import life step
Import_Life_ID=`sbatch \
         --partition=${partition_Insurance} --time=${time_Insurance} \
         --nodes=${nodes} --ntasks=${ntasks_Insurance} --job-name=Import_Life \
		 --output="${erroroutput_path}/Import_Life-%A_%a.out" --error="${erroroutput_path}/Import_Life-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Insurance} \
         --array="1-64" \
         --depend=afterok:${Enumerate_Insurers_ID} \
         "${code_path}/insurance/Insurance_Stata_Controller.sh" ${U} Import_Life | awk '{print $NF}'`
echo "Submitted Import_Life Job: "${Import_Life_ID}
sleep 1

# Import health step
Import_Health_ID=`sbatch \
         --partition=${partition_Insurance} --time=${time_Insurance} \
         --nodes=${nodes} --ntasks=${ntasks_Insurance} --job-name=Import_Health \
         --output="${erroroutput_path}/Import_Health-%A_%a.out" --error="${erroroutput_path}/Import_Health-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Insurance} \
         --array="1-64" \
         --depend=afterok:${Enumerate_Insurers_ID} \
         "${code_path}/insurance/Insurance_Stata_Controller.sh" ${U} Import_Health | awk '{print $NF}'`
echo "Submitted Import_Health Job: "${Import_Health_ID}
sleep 1

# Import p&c step
Import_PC_ID=`sbatch \
         --partition=${partition_Insurance} --time=${time_Insurance} \
         --nodes=${nodes} --ntasks=${ntasks_Insurance} --job-name=Import_PC \
         --output="${erroroutput_path}/Import_PC-%A_%a.out" --error="${erroroutput_path}/Import_PC-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Insurance} \
         --array="1-64" \
         --depend=afterok:${Enumerate_Insurers_ID} \
         "${code_path}/insurance/Insurance_Stata_Controller.sh" ${U} Import_PC | awk '{print $NF}'`
echo "Submitted Import_PC Job: "${Import_PC_ID}
sleep 1

# Consolidate holdings step
Consolidate_Insurance_Holdings_ID=`sbatch \
         --partition=${partition_Insurance} --time=${time_Insurance} \
         --nodes=${nodes} --ntasks=${ntasks_Insurance} --job-name=Consolidate_Insurance_Holdings \
         --output="${erroroutput_path}/Consolidate_Insurance_Holdings-%A_%a.out" --error="${erroroutput_path}/Consolidate_Insurance_Holdings-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Insurance} \
         --depend=afterok:${Import_Life_ID}:${Import_Health_ID}:${Import_PC_ID} \
         "${code_path}/insurance/Insurance_Stata_Controller.sh" ${U} Consolidate_Insurance_Holdings | awk '{print $NF}'`
echo "Submitted Consolidate_Insurance_Holdings Job: "${Consolidate_Insurance_Holdings_ID}
sleep 1

# Industry aggregates step
Industry_Aggregates_ID=`sbatch \
         --partition=${partition_Insurance} --time=${time_Insurance} \
         --nodes=${nodes} --ntasks=${ntasks_Insurance} --job-name=Industry_Aggregates \
         --output="${erroroutput_path}/Industry_Aggregates-%A_%a.out" --error="${erroroutput_path}/Industry_Aggregates-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Insurance} \
         --depend=afterok:${Consolidate_Insurance_Holdings_ID} \
         "${code_path}/insurance/Insurance_Stata_Controller.sh" ${U} Industry_Aggregates | awk '{print $NF}'`
echo "Submitted Industry_Aggregates Job: "${Industry_Aggregates_ID}
sleep 1

echo "All jobs submitted!"
